//:C07:MemTest.cpp
// Testing the Mem class
// {L} Mem
#include "Mem.h"
#include <cstring>
#include <iostream>
using namespace std;

class MyString{
    Mem *buf;
    public:
        MyString();
        MyString(char *str);
        ~MyString();
        void concat(char *str);
        void print(ostream &os);
};

MyString::MyString(){
    buf = 0;
}

MyString::MyString(char *str){
    buf = new Mem(strlen(str) + 1);
    strcpy((char *)buf->pointer(), str);
}

void MyString::concat(char *str){
    if(!buf){
        buf = new Mem;
    }

    strcat((char*)buf->pointer(
        buf->msize() + strlen(str) + 1), str);
}

void MyString::print(ostream &os){
    if(!buf){
        return;
    }
    os << buf->pointer() << '\n';

}

MyString::~MyString(){
    delete buf;
}

int main(void){
    MyString s("My test string");
    s.print(cout);
    s.concat(" some additional stuff");
    s.print(cout);

    MyString s2;
    s2.concat("Using default constructor");
    s2.print(cout);
}
